package com.apex.sdk.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.alibaba.fastjson.JSONObject;
import com.apex.bluetooth.callback.GeneralCallback;
import com.apex.bluetooth.callback.TitanPushSwitchCallback;
import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apex.bluetooth.enumeration.QueryWatchInfoType;
import com.apex.bluetooth.model.TitanNotifyPushSwitch;
import com.apex.bluetooth.model.TitanPushSwitch;
import com.apex.sdk.R;
import com.apex.sdk.databinding.ActivityTitanPushSwitchBinding;
import com.apex.sdk.dialog.SwitchDialog;
import com.apex.sdk.dialog.WaitingDialog;


public class TitanPushSwitchActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();
    private WaitingDialog waitingDialog;
    SwitchDialog callDialog, smsDialog, faceDialog;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    waitingDialog.dismiss();
                }
                TitanPushSwitch titanPushSwitch = (TitanPushSwitch) msg.obj;
                if (titanPushSwitch != null) {
                    Log.e(TAG, "获取到的数据:" + JSONObject.toJSONString(titanPushSwitch));
                    activityTitanPushSwitchBinding.call.setText(titanPushSwitch.getIncomingCallSwitch() == 1 ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close));
                    activityTitanPushSwitchBinding.unknownCall.setText(titanPushSwitch.getMissedCallSwitch() == 1 ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close));
                    activityTitanPushSwitchBinding.email.setText(titanPushSwitch.getEmailSwitch() == 1 ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close));
                    activityTitanPushSwitchBinding.sms.setText(titanPushSwitch.getSmsSwitch() == 1 ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close));
                    activityTitanPushSwitchBinding.socialContact.setText(titanPushSwitch.getSocialSwitch() == 1 ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close));
                    activityTitanPushSwitchBinding.schedule.setText(titanPushSwitch.getScheduleSwitch() == 1 ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close));
                    activityTitanPushSwitchBinding.faceBook.setText(titanPushSwitch.getFaceBookSwitch() == 1 ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close));
                }
            } else if (msg.what == 0x41) {
                if (waitingDialog != null) {
                    waitingDialog.dismiss();
                }
                Toast.makeText(TitanPushSwitchActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    waitingDialog.dismiss();
                }
            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    waitingDialog.dismiss();
                }
                Toast.makeText(TitanPushSwitchActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityTitanPushSwitchBinding activityTitanPushSwitchBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityTitanPushSwitchBinding = ActivityTitanPushSwitchBinding.inflate(getLayoutInflater());
        setContentView(activityTitanPushSwitchBinding.getRoot());
        activityTitanPushSwitchBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityTitanPushSwitchBinding.tool);
        getSupportActionBar().setTitle(null);
        activityTitanPushSwitchBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        EABleConnectState state = EABleManager.getInstance().getDeviceConnectState();
        if (state == EABleConnectState.STATE_CONNECTED) {
            if (waitingDialog == null) {
                waitingDialog = new WaitingDialog(TitanPushSwitchActivity.this);
            }
            waitingDialog.show();
            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.titan_notify_switch, new TitanPushSwitchCallback() {
                @Override
                public void pushSwitch(TitanPushSwitch titanPushSwitch) {
                    if (mHandler != null) {
                        Message message = mHandler.obtainMessage();
                        message.what = 0x40;
                        message.obj = titanPushSwitch;
                        mHandler.sendMessage(message);
                    }
                }

                @Override
                public void mutualFail(int errorCode) {
                    if (mHandler != null) {
                        mHandler.sendEmptyMessage(0x41);
                    }

                }
            });
        }
        activityTitanPushSwitchBinding.call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EABleConnectState state = EABleManager.getInstance().getDeviceConnectState();
                if (state == EABleConnectState.STATE_CONNECTED) {
                    if (callDialog == null) {
                        callDialog = new SwitchDialog(TitanPushSwitchActivity.this);
                        callDialog.setSelectListener(new SwitchDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanPushSwitchBinding.call.setText(sex);
                                TitanNotifyPushSwitch titanPushSwitch = new TitanNotifyPushSwitch();

                                if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                    titanPushSwitch.setSw(1);
                                } else {
                                    titanPushSwitch.setSw(0);
                                }
                                titanPushSwitch.setNotifyType(TitanNotifyPushSwitch.NotifyType.INCOMING_CALL);
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanPushSwitchActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().setTitanPushSwitch(titanPushSwitch, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x42);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x43);
                                        }
                                    }
                                });
                            }
                        });
                    }
                    if (!callDialog.isShowing()) {
                        callDialog.show();
                    }
                }
            }
        });
        activityTitanPushSwitchBinding.sms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EABleConnectState state = EABleManager.getInstance().getDeviceConnectState();
                if (state == EABleConnectState.STATE_CONNECTED) {
                    if (smsDialog == null) {
                        smsDialog = new SwitchDialog(TitanPushSwitchActivity.this);
                        smsDialog.setSelectListener(new SwitchDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanPushSwitchBinding.sms.setText(sex);
                                TitanNotifyPushSwitch titanPushSwitch = new TitanNotifyPushSwitch();
                                if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                    titanPushSwitch.setSw(1);
                                } else {
                                    titanPushSwitch.setSw(0);
                                }
                                titanPushSwitch.setNotifyType(TitanNotifyPushSwitch.NotifyType.SMS);
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanPushSwitchActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().setTitanPushSwitch(titanPushSwitch, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x42);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x43);
                                        }
                                    }
                                });
                            }
                        });
                    }
                    if (!smsDialog.isShowing()) {
                        smsDialog.show();
                    }
                }
            }
        });
        activityTitanPushSwitchBinding.faceBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EABleConnectState state = EABleManager.getInstance().getDeviceConnectState();
                if (state == EABleConnectState.STATE_CONNECTED) {
                    if (faceDialog == null) {
                        faceDialog = new SwitchDialog(TitanPushSwitchActivity.this);
                        faceDialog.setSelectListener(new SwitchDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {
                                activityTitanPushSwitchBinding.faceBook.setText(sex);
                                TitanNotifyPushSwitch titanPushSwitch = new TitanNotifyPushSwitch();
                                if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                    titanPushSwitch.setSw(1);
                                } else {
                                    titanPushSwitch.setSw(0);
                                }
                                titanPushSwitch.setNotifyType(TitanNotifyPushSwitch.NotifyType.FACEBOOK);
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanPushSwitchActivity.this);
                                }
                                waitingDialog.show();
                                EABleManager.getInstance().setTitanPushSwitch(titanPushSwitch, new GeneralCallback() {
                                    @Override
                                    public void result(boolean success, int reason) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x42);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int errorCode) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x43);
                                        }
                                    }
                                });
                            }
                        });
                    }
                    if (!faceDialog.isShowing()) {
                        faceDialog.show();
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        if (callDialog != null) {
            callDialog.dismiss();
            callDialog.destroyDialog();
            callDialog = null;
        }
        if (smsDialog != null) {
            smsDialog.dismiss();
            smsDialog.destroyDialog();
            smsDialog = null;
        }
        if (faceDialog != null) {
            faceDialog.dismiss();
            faceDialog.destroyDialog();
            faceDialog = null;
        }
        super.onDestroy();
    }
}
